package it.mp.multitask;

import it.mp.multitask.bean.App;
import it.mp.multitask.view.LinearLayoutElencoTask;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RecentTaskInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;

public class Main extends Activity {

	private static final int NUMERO_PROCESSI = 20;

	protected static final String SP_OPTIONS = "SP_OPTIONS";
	protected static final String KILL_SENZA_CONFERMA = "KILL_SENZA_CONFERMA";
	protected static final String PACKAGE_NAME = "PACKAGE_NAME->";
	protected static final String APP_SERVICES = "APP_SERVICES->";
	protected static final String APP_RUNNING = "APP_RUNNING->";
	protected static final String APP_RECENTLY = "APP_RECENTLY->";

	private OnClickListener oclStartActivity = new OnClickListener() {
		public void onClick(View v) {
			try {
				App app = (App) v.getTag();
				String packageName = app.getPackageName();
				String className = app.getClassName();
				Intent i = new Intent();
				i.setClassName(packageName, className);
				i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
				finish();
			} catch (Exception ex) {

			}

		}
	};
	private OnClickListener oclFinishActivity = new OnClickListener() {
		public void onClick(View v) {
			finish();
		}
	};
	private OnLongClickListener oclKillActivity = new OnLongClickListener() {
		public boolean onLongClick(View v) {

			SharedPreferences prefs = getSharedPreferences(Main.SP_OPTIONS, Context.MODE_PRIVATE);
			if (!prefs.getBoolean(KILL_SENZA_CONFERMA, false)) {
				try {
					Intent i = new Intent(Main.this, DialogoKill.class);
					App app = (App) v.getTag();
					String packageName = app.getPackageName();
					i.putExtra(Main.PACKAGE_NAME, packageName);
					startActivityForResult(i, 123456);
				} catch (Exception ex) {

				}
			} else {
				killApplication(v);
			}
			return true;
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		crea();
	}

	private void crea() {
		setContentView(R.layout.main); 
		findViewById(R.id.LL_parent).setOnClickListener(oclFinishActivity);
		LinearLayoutElencoTask linearLayout = (LinearLayoutElencoTask) findViewById(R.id.LL_task);		
		Display display = getWindowManager().getDefaultDisplay();				
		linearLayout.setDisplayWidth(display.getWidth());

		ActivityManager actvityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
		SharedPreferences prefs = getSharedPreferences(Main.SP_OPTIONS, Context.MODE_PRIVATE);

		HashSet<String> hs = new HashSet<String>();
		ArrayList<App> alApp = new ArrayList<App>();
		hs.add(this.getPackageName());
		App app = null;

		int size = 0;
		if (prefs.getBoolean(Main.APP_RUNNING, true)) {
			List<RunningTaskInfo> procInfos = actvityManager.getRunningTasks(NUMERO_PROCESSI);
			size = procInfos.size();
			for (int i = 0; i < size; i++) {
				app = new App(procInfos.get(i));
				if (!hs.contains(app.getPackageName())) {
					alApp.add(app);
					hs.add(app.getPackageName());
				}
			}
		}
		int fineAppRunning = alApp.size();
		if (prefs.getBoolean(Main.APP_RECENTLY, true)) {
			List<RecentTaskInfo> recInfos = actvityManager.getRecentTasks(NUMERO_PROCESSI + hs.size(), 0);
			size = recInfos.size();
			for (int i = 0; i < size; i++) {
				app = new App(recInfos.get(i));
				if (!hs.contains(app.getPackageName())) {
					try{
						linearLayout.addView(app.getView(this, oclStartActivity), new LinearLayout.LayoutParams(linearLayout.getLayoutParams().width, linearLayout.getLayoutParams().height));
						hs.add(app.getPackageName());
						
					}catch(NameNotFoundException nnfex){
						
					}
				}
			}
		}		
		size = hs.size();		
		if (size != fineAppRunning) {
			linearLayout.addView(getLayoutInflater().inflate(R.layout.app_separatore, null));
		}		
		for (int i = 0; i < fineAppRunning; i++) {
			try {
				linearLayout.addView(alApp.get(i).getView(this, oclStartActivity, oclKillActivity));
			} catch (NameNotFoundException e) {
			}
		}		
	}

	private void killApplication(View v) {
		App app = (App) v.getTag();
		String packageName = app.getPackageName();
		ActivityManager actvityManager = (ActivityManager) Main.this.getSystemService(ACTIVITY_SERVICE);
		actvityManager.restartPackage(packageName);
		crea();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		crea();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		crea();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {	
		super.onStart();
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		Intent i = new Intent(Main.this, Setup.class);				
		startActivityForResult(i, 1234567);
		
		return super.onPrepareOptionsMenu(menu);
	}
}